package scc.Telas;

import Dao.UserDAO;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Davidson
 */
public class frmReimpressaoRecibo extends javax.swing.JInternalFrame {

    private static frmReimpressaoRecibo tela;
    private static UserDAO dao;
    
    /** Creates new form frmManutencaoDeUsuarios */
    private frmReimpressaoRecibo() {
        initComponents();
        this.setVisible(true);
        this.moveToFront();
        ftfData.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
    }
    
    public static frmReimpressaoRecibo getInstancia(UserDAO userDao){
        if (tela == null) {
            dao = userDao;
            tela = new frmReimpressaoRecibo();
        }
        return tela;
    }    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgTurnos = new javax.swing.ButtonGroup();
        bgRecibos = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ftfData = new javax.swing.JFormattedTextField();
        imgData = new javax.swing.JLabel();
        btExecutar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        rbTurno1 = new javax.swing.JRadioButton();
        rbTurno2 = new javax.swing.JRadioButton();
        rbTurno3 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        rbReciboAbertura = new javax.swing.JRadioButton();
        rbReciboFechamento = new javax.swing.JRadioButton();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Reimpressão de Recibos");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/scc/Imagens/currency_dollar_red.png"))); // NOI18N
        setMinimumSize(new java.awt.Dimension(758, 361));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Selecione a Data");

        ftfData.setForeground(new java.awt.Color(51, 51, 51));
        try {
            ftfData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ftfData.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        ftfData.setMaximumSize(new java.awt.Dimension(70, 28));
        ftfData.setMinimumSize(new java.awt.Dimension(70, 28));
        ftfData.setPreferredSize(new java.awt.Dimension(70, 28));

        imgData.setIcon(new javax.swing.ImageIcon(getClass().getResource("/scc/Imagens/calendar.png"))); // NOI18N
        imgData.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        imgData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imgDataMouseClicked(evt);
            }
        });

        btExecutar.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btExecutar.setText("Executar");
        btExecutar.setPreferredSize(new java.awt.Dimension(99, 30));
        btExecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExecutarActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Turno:");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        bgTurnos.add(rbTurno1);
        rbTurno1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        rbTurno1.setForeground(new java.awt.Color(51, 51, 51));
        rbTurno1.setSelected(true);
        rbTurno1.setText("Turno 1");
        rbTurno1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        bgTurnos.add(rbTurno2);
        rbTurno2.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        rbTurno2.setForeground(new java.awt.Color(51, 51, 51));
        rbTurno2.setText("Turno 2");
        rbTurno2.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        bgTurnos.add(rbTurno3);
        rbTurno3.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        rbTurno3.setForeground(new java.awt.Color(51, 51, 51));
        rbTurno3.setText("Turno 3");
        rbTurno3.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbTurno1)
                    .addComponent(rbTurno2)
                    .addComponent(rbTurno3))
                .addContainerGap(167, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rbTurno1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbTurno2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbTurno3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Recibo:");

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        bgRecibos.add(rbReciboAbertura);
        rbReciboAbertura.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        rbReciboAbertura.setForeground(new java.awt.Color(51, 51, 51));
        rbReciboAbertura.setSelected(true);
        rbReciboAbertura.setText("Recibo de Abertura");
        rbReciboAbertura.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        bgRecibos.add(rbReciboFechamento);
        rbReciboFechamento.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        rbReciboFechamento.setForeground(new java.awt.Color(51, 51, 51));
        rbReciboFechamento.setText("Recibo de Fechamento");
        rbReciboFechamento.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbReciboAbertura)
                    .addComponent(rbReciboFechamento))
                .addContainerGap(167, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rbReciboAbertura)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbReciboFechamento)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(ftfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(2, 2, 2)
                                .addComponent(imgData, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btExecutar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(1, 1, 1)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(2, 2, 2)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(ftfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(imgData, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addGap(1, 1, 1)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(btExecutar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(102, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void imgDataMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgDataMouseClicked

        frmCalendar dialog = new frmCalendar(new javax.swing.JFrame(), false, ftfData);
        int x = getBounds().x;
        int y = getBounds().y;

        dialog.setBounds(x + 30, y + 155, 224, 140);
        dialog.setVisible(true);
    }//GEN-LAST:event_imgDataMouseClicked

    private void btExecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExecutarActionPerformed
        String d = ftfData.getText();        
        String dt = d.substring(6,10)+"-"+d.substring(3,5)+"-"+d.substring(0,2);//+" "+d.substring(13)+".000";
        
        int turno = 0;
        if (rbTurno1.isSelected()) turno = 1;
        if (rbTurno2.isSelected()) turno = 2;
        if (rbTurno3.isSelected()) turno = 3;
        
        String file = "";
        String titulo = "";
        
        if (rbReciboAbertura.isSelected()) {
            file = "./jasper/ReimpressaoReciboAberturaCaixa.jasper";
            titulo = "Reimpressão - Recibo de Abertura";
        } else {
            file = "./jasper/ReimpressaoReciboFechamentoCaixa.jasper";
            titulo = "Reimpressão - Recibo de Fechamento";            
        }

        JasperPrint jp;
        try {
            HashMap parametros = new HashMap();
            parametros.put("TURNO", turno);
            parametros.put("DATA", dt);
            jp = JasperFillManager.fillReport(file, parametros, dao.getConnection());
            JasperViewer jv = new JasperViewer(jp, false);
            jv.setTitle(titulo);
            JDialog view = new JDialog(new javax.swing.JFrame(), titulo, true);
            Toolkit tollkit = Toolkit.getDefaultToolkit();
            Dimension dimensao = tollkit.getScreenSize();
            view.setSize((int)dimensao.getWidth(), (int)(dimensao.getHeight()*0.95));
            view.getContentPane().add(jv.getContentPane());
            view.setVisible(true);            
        } catch (JRException ex) {
            Logger.getLogger(frmCaixa.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this.getParent(), ex.toString());
        }
    }//GEN-LAST:event_btExecutarActionPerformed
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgRecibos;
    private javax.swing.ButtonGroup bgTurnos;
    private javax.swing.JButton btExecutar;
    private javax.swing.JFormattedTextField ftfData;
    private javax.swing.JLabel imgData;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton rbReciboAbertura;
    private javax.swing.JRadioButton rbReciboFechamento;
    private javax.swing.JRadioButton rbTurno1;
    private javax.swing.JRadioButton rbTurno2;
    private javax.swing.JRadioButton rbTurno3;
    // End of variables declaration//GEN-END:variables
}
